/**
	Constructs a TextField and invis MovieClip based button inside a new clip in a specified location

	@author Matthew Richmond
	@version 2006-05-21

	@usage

	- Activate with:
	<code>
	
		// make a set of buttons
		var links = new Array();
		links = ["Huzzah", "Pico", "Taco", "Burrito", "Photo"]

		var xpos = 50;

		for ( var i=0; i<links.length; i++ ) {
	
			var foo:TextFieldButton = new TextFieldButton(this);
			foo.setFontSymbol("Interstate_fnt");
			foo.setFontSize (14)
			foo.setBaseColor (0xFF6600)
			foo.setHoverColor (0x00000)
			foo.create(links[i]);
	
			foo.setX(xpos);
			foo.setY(150);
			foo.id = i;
	
			xpos += (foo.getWidth() + 20);
	
			var ears = {onRelease: function(id) { trace("Done Biznitch! for thing "+id);}}
			foo.addListener(ears)
		};

		// make just one button
		var bar:TextFieldButton = new TextFieldButton(this);
		bar.setFontSymbol("Interstate_fnt");
		bar.create("Lonely Button", 100, 100);

		var ears = {onRelease: this.test1}
		bar.addListener(ears)

		function test1 (id, obj){
			//trace("test1 from id:"+id+", obj:"+obj);
			trace("Sending a 'select' back into your instance of TextFieldButton");
			obj.select();
		};
		
	</code>

*/
dynamic class timber.ui.TextFieldButton {

	// passed
	private var obj:MovieClip;
	private var copy:String;
	public var id:Number;
	
	// core
	private var mc:MovieClip;	
	private var txt:TextField;
	private var but:MovieClip;
	private var xpos:Number;
	private var ypos:Number;
	
	private var defaultFont:String;
	private var defaultFontSize:Number;
	private var defaultEmbedFonts:Boolean = false;
	private var defaultOverage:Number = 4; //sometimes the box/text measurement is off
	
	private var baseFormat:TextFormat;
	private var customFormat:TextFormat;
	private var hoverFormat:TextFormat;
	private var selectFormat:TextFormat;
	
	private var hover:Boolean = true;
	
	private var wrap:Boolean = false;
	private var wrapWidth:Number;
	
	private var isPersistent:Boolean = false; // if the dusabled button still links

// ===========================================================
// - CONSTRUCTOR
// ===========================================================

	function TextFieldButton (obj:MovieClip) {
		//trace("TextFieldButton");
		AsBroadcaster.initialize(this);
		
		this.obj = obj;
		
		setDefaults ();
	};

// ===========================================================
// - ACCESSORS
// ===========================================================

	function select (){
		//trace("select");
		
		txt.setTextFormat(selectFormat);
		if (!isPersistent) but.enabled = false;
	};

	function deselect (){
		//trace("deselect");
		
		txt.setTextFormat(baseFormat);
		but.enabled = true;
	};
	
	function setPersistent (choice:Boolean){
		trace("setPersistent");
		
		isPersistent = choice;
	};
	
	function setX (value:Number){
		//trace("setX");
		mc._x = value;
	};
	
	function setY (value:Number){
		//trace("setY");
		mc._y = value;
	};

	function getX (){
		//trace("getX");
		return mc._x;
	};
	
	function getY (){
		//trace("getY");
		return mc._y;
	};

	function getWidth (){
		//trace("getWidth");
		return txt.textWidth;
	};

	function getHeight (){
		//trace("getHeight");
		return txt.textHeight;
	};

	function setWrap (value:Number){
		//trace("setWrap");
		
		wrap = true;
		wrapWidth = value;
		
	};

	function setHover (choice:Boolean){
		//trace("setHover");
		hover = choice;
	};

	function setBaseFormat (choice:TextFormat){ // replace text format object
		//trace("setBaseFormat");
		baseFormat = choice;
		defaultEmbedFonts = true;
	};

	function setFontSymbol (target:String){
		//trace("setFontSymbol");
		baseFormat.font = target;
		defaultEmbedFonts = true;
	};
	
	function setFontSize (choice:Number){
		//trace("setFontSize");
		baseFormat.size = choice;
	};
	
	function setBaseColor (choice:Number){
		//trace("setBaseColor");
		baseFormat.color = choice;
	};
	
	function setHoverColor (choice:Number){
		//trace("setHoverColor");
		hoverFormat.color = choice;
	};
	
	function setSelectColor (choice:Number){
		//trace("setSelectColor");
		selectFormat.color = choice;
	};

// ===========================================================
// - EVERYTHING ELSE
// ===========================================================

	function setDefaults (){
		//trace("setDefaults");
	
		// Create default TextFormat object.
		baseFormat = new TextFormat();
		baseFormat.font = "Arial";
		baseFormat.color = 0x000000; // hex value for black 
		baseFormat.size = 21;
	
		hoverFormat = new TextFormat();
		hoverFormat.color = 0xFF0000; // hex value for red 

		selectFormat = new TextFormat();
		selectFormat.color = 0xFF6600; // hex value for red 

	};
	
	function create (copy:String, xpos:Number, ypos:Number){
		//trace("create");
		
		if (!xpos) var xpos = 0;
		this.xpos = xpos;
		
		if (!ypos) var ypos = 0;
		this.ypos = ypos;
		
		// make a clip to hold the entire thing
		var depth:Number = obj.getNextHighestDepth();
		mc = obj.createEmptyMovieClip("tfb"+depth+"_mc", depth);
		mc._x = xpos;
		mc._y = ypos;
		
		// are we wraping, if so we need to set width
		if (wrap) {
			var initw = wrapWidth;
		} else {
			var initw = 20
		}
		
		// make the text field
		txt = mc.createTextField("field_txt", 1, 0, 0, initw, 20)
		
		// text field properties
		txt.selectable = false;
		/*txt.border = true;
		txt.borderColor = 0x000000;*/
		
		// do we need to wrap
		if (wrap) txt.wordWrap = true;
		
		txt.autoSize = true;
		txt.text = copy;
		txt.embedFonts = defaultEmbedFonts;
		txt.setTextFormat(baseFormat);
		
		// make button mc
		but = mc.createEmptyMovieClip("button_mc", 2);
		but._x = 0;
		but._y = 0;
		
		// draw the box
		var w = txt.textWidth + defaultOverage;
		var h = txt.textHeight + defaultOverage;
	
		but.beginFill(0xFF6600,0);
		but.moveTo(0, 0);
		but.lineTo(w, 0);
		but.lineTo(w, h);
		but.lineTo(0, h);
		but.lineTo(0, 0);
		but.endFill();
		
		// set up hover and over
		if (hover) createHover ();
		if (hover) createRelease ();
	};
	
	function createHover (){
		//trace("createHover");
		
		but.txt = txt;
		but.self = this;
		
		but.onRollOver = but.onDragOver = function() {
			this.txt.setTextFormat(this.self.hoverFormat);
			this.self.broadcastMessage("onRollOver", this.self.id, this.self);
		}
		
		but.onRollOut = but.onDragOut = function() {
			this.txt.setTextFormat(this.self.baseFormat);
			this.self.broadcastMessage("onRollOut", this.self.id, this.self);
		}
	};
	
	function createRelease (){
		//trace("createRelease");
		
		but.onRelease = but.onReleaseOutside = function() {
			this.self.broadcastMessage("onRelease", this.self.id, this.self);
		}
		
	};
};